package com.cincc.cinccimdemo.ui.main;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.ui.conversation.ConversationActivity;
import com.cincc.cinccimdemo.widget.OptionItemView;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.UserDetailInfo;

public class UserInfoActivity extends IMBaseActivity {
    private static final String TAG = "UserDetailActivity";
    String userId = "";

    UserDetailInfo userDetailInfo;

    @Override
    protected int contentLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    protected int menu() {
        return R.menu.user_detial;
    }

    TextView titleTextView;
    TextView displayNameTextView;
    LinearLayout chatLayout;
    FrameLayout voipChatLayout;
    Button inviteBtn;
    OptionItemView optionItemView;

    MenuItem itemDelete;
    MenuItem itemAddFriend;
    MenuItem itemAddBlacklist;
    MenuItem itemRemoveBlacklist;
    ImageView portraitImageView;

    @Override
    protected void bindViews() {
        super.bindViews();
        userId = getIntent().getStringExtra(AttrConstants.USER_ID);

        chatLayout = findViewById(R.id.chatButton);
        voipChatLayout = findViewById(R.id.voipChatButton);
        inviteBtn = findViewById(R.id.inviteButton);
        titleTextView = findViewById(R.id.titleTextView);
        displayNameTextView = findViewById(R.id.displayNameTextView);
        TextView accountText = findViewById(R.id.accountTextView);
        portraitImageView = findViewById(R.id.portraitImageView);
        optionItemView = findViewById(R.id.aliasOptionItemView);

        accountText.setText("ID:" + userId);

        optionItemView.setOnClickListener(view -> setAlias());
        chatLayout.setOnClickListener(view -> openConversation());
    }

    private void setAlias() {
        Intent i = new Intent(this, SetAliasActivity.class);
        i.putExtra(AttrConstants.USER_ID, userId);
        i.putExtra(AttrConstants.ALIAS, TextUtils.isEmpty(userDetailInfo.alias) ? userDetailInfo.nickName : userDetailInfo.alias);
        startActivity(i);
    }

    private void openConversation() {
        ConversationActivity.startSingleConversation(this, userId, TextUtils.isEmpty(userDetailInfo.alias) ? userDetailInfo.nickName : userDetailInfo.alias);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    private void refreshData() {
        IMSipPhoneCtrl.getInstance().getUserDetailInfo(userId, imResponse -> {
            runOnUiThread(() -> {
                if (imResponse.getCode() == 0) {
                    userDetailInfo = (UserDetailInfo) imResponse.getData();
                    refreshUI(userDetailInfo);
                }
            });
        });
    }

    private void refreshUI(UserDetailInfo userDetailInfo) {
        if (TextUtils.isEmpty(userDetailInfo.alias)) {
            displayNameTextView.setVisibility(View.GONE);
            titleTextView.setText(userDetailInfo.nickName);
        } else {
            titleTextView.setText(userDetailInfo.alias);
            displayNameTextView.setText("昵称:" + userDetailInfo.nickName);
        }

        Glide.with(this).load(userDetailInfo.protraitUrl).placeholder(R.mipmap.avatar_def)
                .transforms(new CenterCrop(), new RoundedCorners(15))
                .into(portraitImageView);

        chatLayout.setVisibility(userDetailInfo.isFriend ? View.VISIBLE : View.GONE);
        voipChatLayout.setVisibility(userDetailInfo.isFriend ? View.VISIBLE : View.GONE);
        inviteBtn.setVisibility(userDetailInfo.isFriend ? View.GONE : View.VISIBLE);
        optionItemView.setVisibility(userDetailInfo.isFriend ? View.VISIBLE : View.GONE);

        itemAddFriend.setEnabled(!userDetailInfo.isFriend);
        itemAddFriend.setVisible(!userDetailInfo.isFriend);
        itemDelete.setEnabled(userDetailInfo.isFriend);
        itemDelete.setVisible(userDetailInfo.isFriend);

        itemAddBlacklist.setEnabled(!userDetailInfo.isInBlack);
        itemAddBlacklist.setVisible(!userDetailInfo.isInBlack);
        itemRemoveBlacklist.setEnabled(userDetailInfo.isInBlack);
        itemRemoveBlacklist.setVisible(userDetailInfo.isInBlack);
    }

    @Override
    protected void bindEvents() {
        super.bindEvents();
        findViewById(R.id.inviteButton).setOnClickListener(_v -> InviteFriendActivity.inviteAddFriend(this, userId));
    }

    @Override
    protected void afterMenus(Menu menu) {
        super.afterMenus(menu);
        itemDelete = menu.findItem(R.id.delete);
        itemAddFriend = menu.findItem(R.id.addFriend);
        itemAddBlacklist = menu.findItem(R.id.addBlacklist);
        itemRemoveBlacklist = menu.findItem(R.id.removeBlacklist);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.delete) {
            applyDelFriend();
        } else if (item.getItemId() == R.id.addFriend) {
            InviteFriendActivity.inviteAddFriend(this, userId);
        } else if (item.getItemId() == R.id.addBlacklist) {
            addBlack();
        } else if (item.getItemId() == R.id.removeBlacklist) {
            delBlack();
        }
        return super.onOptionsItemSelected(item);
    }

    private void applyDelFriend() {
        IMSipPhoneCtrl.getInstance().applyDelFriend(userId, imResponse -> {
            String result = imResponse.getCode() == 0 ? "申请删除好友成功" : imResponse.getMsg();
            refreshData();
            runOnUiThread(() -> Toast.makeText(UserInfoActivity.this, result, Toast.LENGTH_SHORT).show());
        });
    }

    private void addBlack() {
        IMSipPhoneCtrl.getInstance().addFriendBlack(userId, imResponse -> {
            String result = imResponse.getCode() == 0 ? "加入黑名单成功" : imResponse.getMsg();
            refreshData();
            runOnUiThread(() -> Toast.makeText(UserInfoActivity.this, result, Toast.LENGTH_SHORT).show());
        });
    }

    private void delBlack() {
        IMSipPhoneCtrl.getInstance().delFriendBlack(userId, imResponse -> {
            String result = imResponse.getCode() == 0 ? "移除黑名单成功" : imResponse.getMsg();
            refreshData();
            runOnUiThread(() -> Toast.makeText(UserInfoActivity.this, result, Toast.LENGTH_SHORT).show());
        });
    }

    public static void startUserDetail(Context context, String userId) {
        Intent i = new Intent(context, UserInfoActivity.class);
        i.putExtra(AttrConstants.USER_ID, userId);
        context.startActivity(i);
    }

}
